package com.linlang.grasharepro.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.dao.UserDB;
import com.linlang.grasharepro.model.User;

/**
 * Created by lixin on 2016-8-8.
 */
public class PersonInfo extends Activity {

    private UserDB userDB;
    private User user;
    private EditText et_displayName,et_loginId,et_sex,et_major,et_school,et_academicStarts,et_graduationTime,et_work,et_location,et_introduction,et_realName,et_birthday,et_contactWay,et_mail;
    private ImageView iv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_info);
        initData();

        TextView release = (TextView) findViewById(R.id.tv_updateUser);
        release.setOnClickListener(new MyOnClickListener());

        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public class MyOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            userDB = new UserDB(PersonInfo.this);
            userDB.open();

            User updateUser = new User(user.getLoginName(),"",et_displayName.getText().toString(),et_realName.getText().toString(),et_sex.getText().toString(),et_birthday.getText().toString(),et_major.getText().toString(),et_school.getText().toString(),et_academicStarts.getText().toString(),et_graduationTime.getText().toString(),et_work.getText().toString(),et_location.getText().toString(),et_contactWay.getText().toString(),et_mail.getText().toString(),et_introduction.getText().toString(),"","");

            int updateIndex = userDB.updateUser(updateUser);
            if(updateIndex!=0){
                Toast.makeText(PersonInfo.this,"修改成功",Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void initData() {
        et_displayName = (EditText)findViewById(R.id.et_displayName);
        et_loginId = (EditText)findViewById(R.id.et_loginId);
        et_sex =(EditText)findViewById(R.id.et_sex);
        et_major =(EditText)findViewById(R.id.et_major);
        et_school =(EditText)findViewById(R.id.et_school);
        et_academicStarts =(EditText)findViewById(R.id.et_academicStarts);
        et_graduationTime =(EditText)findViewById(R.id.et_graduationTime);
        et_work =(EditText)findViewById(R.id.et_work);
        et_location =(EditText)findViewById(R.id.et_location);
        et_introduction =(EditText)findViewById(R.id.et_introduction);
        et_realName =(EditText)findViewById(R.id.et_realName);
        et_birthday =(EditText)findViewById(R.id.et_birthday);
        et_contactWay =(EditText)findViewById(R.id.et_contactWay);
        et_mail =(EditText)findViewById(R.id.et_mail);


        //获取用户登陆信息
        SharedPreferences preferences = getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        String account = preferences.getString("account", "");

        userDB = new UserDB(this);
        userDB.open();

        user = userDB.getUser(account);

        et_displayName.setText(user.getDisplayName());
        et_loginId.setText(user.getLoginName());
        et_sex.setText(user.getSex());
        et_major.setText(user.getMajor());
        et_school.setText(user.getSchool());
        et_academicStarts.setText(user.getAcademicStarts());
        et_graduationTime.setText(user.getGraduationTime());
        et_work.setText(user.getWork());
        et_location.setText(user.getLocation());
        et_introduction.setText(user.getIntroduction());
        et_realName.setText(user.getRealName());
        et_birthday.setText(user.getBirthday());
        et_contactWay.setText(user.getContactWay());
        et_mail.setText(user.getMail());

    }
}
